﻿using System;
using System.Windows.Forms;
using StockLib;
using StockUI.Properties;

namespace StockUI
{
    public partial class DialogOptionBlock : DialogAbstract
    {
        private FormKey _formKey = null;

        public DialogOptionBlock()
        {
            InitializeComponent();

            imageList.Images.Add(Resources.folder);
            imageList.Images.Add(Resources.doc);

            listBlock.PaintItem = delegate(ListViewItem item, object data)
            {
                StockBlock block = data as StockBlock;

                item.Text = string.Format("{0} ({1})", block.Name, block.StockNumber);
                item.Tag = block;
                item.ImageIndex = 0;
            };

            listStock.PaintItem = delegate(ListViewItem item, object data)
            {
                Stock stock = data as Stock;

                item.Text = stock.Name;
                item.Tag = stock;
                item.ImageIndex = 1;
            };

            StockBlockGroupCustom.Instance.DataChanged += new StockBlockGroupDataEventHandler(StockBlockGroupCustom_DataChanged);

            StockBlockGroupCustom.Instance.StockDataChanged += new StockBlockGroupStockDataEventHandler(StockBlockGroupCustom_StockDataChanged);
        }

        private void StockBlockGroupCustom_StockDataChanged(object sender, EventArgs e)
        {
            listBlock.Bind();
            listStock.Bind();
        }

        private void StockBlockGroupCustom_DataChanged(object sender, EventArgs e)
        {
            listBlock.Bind();
            listStock.Bind();
        }

        private void DialogOptionBlock_Load(object sender, System.EventArgs e)
        {
            listBlock.DataSource = StockBlockGroupCustom.Instance.Values;
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            DialogStockBlockAdd dialog = new DialogStockBlockAdd();
            dialog.ShowDialog();
        }


        private void listBlock_DoubleClick(object sender, EventArgs e)
        {
            btnDelete_Click(sender, e);
        }

        private void btnDelete_Click(object sender, System.EventArgs e)
        {
            if (!CheckBlockSelected())
                return;

            StockBlock block = listBlock.SelectedItems[0].Tag as StockBlock;
            if (block.BlockId == StockBlockGroupCustom.Instance.ZXGBlock.BlockId)
            {
                MessageBox.Show(this, "不能删除自选股！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            if (MessageBox.Show(this, "确定删除该版块吗？", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                if (StockBlockGroupCustom.Instance.ContainsKey(block.BlockId))
                {
                    StockBlockGroupCustom.Instance.Remove(block.BlockId);
                    StockBlockGroupCustom.Instance.RaiseDataChanged(sender, e);
                }
            }
        }

        private void listBlock_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBlock.SelectedItems.Count <= 0)
                return;

            StockBlock block = listBlock.SelectedItems[0].Tag as StockBlock;
            listStock.DataSource = block.Values;
            listStock.Tag = block;
        }

        private bool CheckBlockSelected()
        {
            if (listBlock.SelectedItems.Count <= 0)
            {
                MessageBox.Show(this, "请选择版块", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }

            return true;
        }

        private void FormKey_Selected(object sender, FormKeySelectedEventArgs e)
        {
            if (e.Stock == null)
                return;

            if (listStock.Tag == null)
                return;

            StockBlock block = listStock.Tag as StockBlock;
            if (block.ContainsKey(e.Stock.CodeKey))
                return;

            block.Add(e.Stock.CodeKey, e.Stock);
            block.StockNumber = block.Count;

            StockBlockGroupCustom.Instance.RaiseStockDataChanged(sender, e);
        }

        private void btnStockAdd_Click(object sender, EventArgs e)
        {
            if (!CheckBlockSelected())
                return;

            if (_formKey == null)
            {
                _formKey = new FormKey();
                _formKey.AutoClose = false;
                _formKey.Selected += new FormKeySelectedEventHandler(FormKey_Selected);
            }
            _formKey.Left = this.Left + this.Width;
            _formKey.Top = this.Top + this.Height - _formKey.Height;
            _formKey.ShowDialog(this);
        }

        private void listStock_DoubleClick(object sender, EventArgs e)
        {
            btnStockDelete_Click(sender, e);
        }

        private void btnStockDelete_Click(object sender, EventArgs e)
        {
            if (!CheckBlockSelected())
                return;

            if (!CheckStockSelected())
                return;

            Stock stock = listStock.SelectedItems[0].Tag as Stock;
            if (MessageBox.Show(this, "确定删除股票吗？", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                if (listStock.Tag != null)
                {
                    StockBlock block = listStock.Tag as StockBlock;
                    block.Remove(stock.CodeKey);
                    block.StockNumber = block.Count;

                    StockBlockGroupCustom.Instance.RaiseStockDataChanged(sender, e);
                }
            }
        }

        private void btnStockClear_Click(object sender, EventArgs e)
        {
            if (!CheckBlockSelected())
                return;

            if (MessageBox.Show(this, "确定清空股票吗？", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
            {
                if (listStock.Tag != null)
                {
                    StockBlock block = listStock.Tag as StockBlock;
                    block.Clear();
                    block.StockNumber = block.Count;

                    StockBlockGroupCustom.Instance.RaiseStockDataChanged(sender, e);
                }
            }
        }

        private bool CheckStockSelected()
        {
            if (listStock.SelectedItems.Count <= 0)
            {
                MessageBox.Show(this, "请选择股票", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }

            return true;
        }
    }
}
